package com.xca.is.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.xca.is.R;
import com.xca.is.adapter.SoftwareAdapter;
import com.xca.is.databinding.ActivitySoftBinding;
import com.xca.is.entity.SoftwareEntity;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.SoftwareModel;
import com.xca.is.utils.EditTextUtils;
import com.xca.is.utils.NotificationUtils;
import com.xca.is.utils.WebViewScriptUtils;

import java.util.ArrayList;
import java.util.List;

public class SoftwareActivity extends AppCompatActivity implements HttpConnect {
    private ActivitySoftBinding binding ;
    private SoftwareAdapter adapter ;
    private List<SoftwareEntity.DataDTO> list ;
    private ViewModelProvider provider ;
    private SoftwareModel model ;
    private MutableLiveData<SoftwareEntity> liveData ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySoftBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        binding.softLoading.rLoading.setVisibility(View.GONE);

        int[] ints = new int[]{10} ;
        WebViewScriptUtils.htmlContent(ints,binding.softwareWeb);

        list = new ArrayList<>() ;
        binding.softwareRecycle.setLayoutManager(new GridLayoutManager(this,1));

        provider = new ViewModelProvider(this) ;
        model = provider.get(SoftwareModel.class) ;
        model.setModel(this);
        liveData = (MutableLiveData<SoftwareEntity>) model.getLiveData() ;
        liveData.observe(this, new Observer<SoftwareEntity>() {
            @Override
            public void onChanged(SoftwareEntity softwareEntity) {
                if (softwareEntity!=null){
                    list = softwareEntity.getData() ;
                    adapter.updateData(list);
                }
            }
        });

        adapter = new SoftwareAdapter(list, this);
        binding.softwareRecycle.setAdapter(adapter);
        onClickListener();
    }


    private void onClickListener()
    {
        binding.softwareSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = binding.softwareEdit.getText().toString().trim() ;
                if (name.isEmpty())
                {
                    binding.softwareEdit.setError("请输入软件名称");
                    return;
                }
                model.getData(name);
                EditTextUtils.clearFocus(binding.softwareEdit);
            }
        });

    }

    @Override
    public void Loading(boolean loading) {
        binding.softLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        binding.softLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}